﻿
using Test30.Util;

namespace Test30.Engine
{
    public class ItemCollection:ThingCollection<Item>
    {
        public Dungeon Dungeon { get; }

        public ItemCollection(Dungeon dungeon)
        {
            Dungeon = dungeon;
        }

        protected override void OnItemAdded(Item item)
        {
            base.OnItemAdded(item);
            ((ICollectible<ItemCollection, Item>)item).SetCollection(this);
            if (item.GivesOffLight)
            {
                Dungeon.DirtyLighting();
            }
        }

        protected override void OnItemRemoved(Item item)
        {
            base.OnItemRemoved(item);
            ((ICollectible<ItemCollection, Item>)item).SetCollection(this);
            if (item.GivesOffLight)
            {
                Dungeon.DirtyLighting();
            }
        }
    }

    public static class ItemCollectionExtensions
    {
        public static int IndexOf(this IItemCollection collection, Item item)
        {
            int index = 0;
            foreach (Item thisItem in collection)
            {
                if (thisItem == item) return index;
                index++;
            }

            return -1;
        }
    }
}
